package cn.zhqr.core.interceptor;

import cn.zhqr.common.bean.response.ErrorCode;
import cn.zhqr.common.bean.response.OutResponse;
import cn.zhqr.common.exception.ErrorCodeException;
import cn.zhqr.common.exception.PermissionDenyException;
import cn.zhqr.common.exception.RepeatSubmissionException;
import cn.zhqr.common.exception.SessionTimeoutException;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.ActionException;
import com.jfinal.kit.StrKit;
import org.apache.log4j.Logger;

import java.security.InvalidParameterException;

/**
 * 捕获系统所有异常的拦截器
 * Created by charles on 2016/9/12.
 */
public class UncaughtExceptionInterceptor implements Interceptor{
    private static final Logger log = Logger.getLogger(UncaughtExceptionInterceptor.class);

    @Override
    public void intercept(Invocation inv) {
        try {
            inv.invoke();
        }catch (Exception e){
            OutResponse response = handleException(e);
            inv.getController().renderJson(response);
        }
    }

    public static OutResponse handleException(Exception e) {
        if (e instanceof InvalidParameterException) {
            //捕获参数检查异常
            String error_msg = StrKit.notBlank(e.getMessage()) ? e.getMessage() : ErrorCode.INVALID_PARAM.getMsg();
            log.error("UncaughtExceptionInterceptor caught an error:", e);
            return OutResponse.buildError(ErrorCode.INVALID_PARAM.getCode(), error_msg);
        } else if (e instanceof ErrorCodeException) {
            //捕获携带错误码的异常，由程序主动抛出
            ErrorCodeException ec = (ErrorCodeException) e;

            if (ec.getCode() == 0) {
                log.error("UncaughtExceptionInterceptor render success json!", e);
                return OutResponse.buildSuccess(ec.getData());
            } else {
                log.error("UncaughtExceptionInterceptor render error json:" + ((ErrorCodeException) e).getMsg(), e);
                return OutResponse.buildError(ec.getCode(), ec.getMsg(), ec.getData());
            }
        } else if (e instanceof SessionTimeoutException) {
            //捕获登录超时异常
            log.error("UncaughtExceptionInterceptor caught an error:", e);
            return OutResponse.buildError(ErrorCode.LOGIN_TIMEOUT);
        } else if (e instanceof PermissionDenyException) {
            //捕获权限不足异常
            log.error("UncaughtExceptionInterceptor caught an error:", e);
            return OutResponse.buildError(ErrorCode.PERMISSION_DENY,
                    StrKit.notBlank(e.getMessage())?e.getMessage():"权限不足，请联系管理员");
        } else if(e instanceof RepeatSubmissionException){
            //捕获重复提交异常
            log.error("UncaughtExceptionInterceptor caught an error:", e);
            String error_msg = StrKit.notBlank(e.getMessage()) ? e.getMessage() : "请勿重复提交";

            return OutResponse.buildError(ErrorCode.REPEAT_SUBMISSION,error_msg,((RepeatSubmissionException) e).getDetail());
        }else if (e instanceof ActionException) {
            //捕获其他异常
            log.error("UncaughtExceptionInterceptor caught an error:", e);
            return OutResponse.buildError(ErrorCode.INVALID_PARAM.code,"参数格式不正确，请检查");
        } else {
            //捕获其他异常
            log.error("UncaughtExceptionInterceptor caught an error:", e);
            return OutResponse.buildError(ErrorCode.SERVER_ERROR);
        }
    }
}
